package com.c.ctgproject.ui.collect

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CarSearchAdapter
import com.c.ctgproject.adapter.guang.DestroyTypeAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityMyInputBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.CarListEntity
import com.c.ctgproject.entity.guang.DestroyPhotoListEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/10/8
 *描述: 我的录入 页面 (车辆)
 */
class MyInputActivity : BaseActivityV2(),CarSearchAdapter.CarSearchCallBack {
    private val mBinding by lazy { ActivityMyInputBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private var mList = ArrayList<CarListEntity.DataBean.ListBean>()
    private val mAdapter by lazy { CarSearchAdapter(this, mList,this) }


    override fun event() {
        getData()
    }

    override fun click() {
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }
    fun getData() {

            //流程类型 1-预录入 2-登记入库 3-过磅 4-初检 5-预处理 6-新建拆解 7-完成拆解 8-毁型拍照
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myCreateHandlerOrder(search,  page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myHandlerCar(search, 1, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的车辆列表:$it")
                if (page == 1) mList.clear()
                val data = getGson().fromJson(it, CarListEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mCarRV.handlerPosition(page)
                }
            })
    }

    override fun deleteItem(position: Int, data: CarListEntity.DataBean.ListBean) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.deleteCar(data.carId), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("删除结果:$it")
            val result= getGson().fromString<ResultEntity<String>>(it)
            if(result.status==1){
                mList.removeAt(position)
                mAdapter.notifyDataSetChanged()
            }
        })
    }
}